<!--
  Copyright © 2015 Cask Data, Inc.

  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy of
  the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  License for the specific language governing permissions and limitations under
  the License.
-->

<div class="modal" tabindex="-1" role="dialog">
  <div class="modal-lg modal-dialog" ng-controller="StatusMakeRequestController as RequestController">
    <div class="modal-content">

      <form name="makeRequestForm"
            class="form-horizontal"
            ng-submit="RequestController.makeRequest()"
            novalidate>

        <div class="modal-header clearfix">
          <div class="pull-left">
            <h3 class="modal-title" ng-bind="RequestController.programId"></h3>
            <p> Type: Service </p>
          </div>
          <a class="btn pull-right" ng-click="$hide()">
            <span class="fa fa-remove"></span>
            <span class="sr-only"> Close </span>
          </a>
        </div>

        <div class="modal-body" >

          <div class="form-group">
            <label class="control-label col-sm-2"> Make a Request </label>
            <div class="col-sm-10">
              <div class="input-group">
                <span class="input-group-addon" ng-bind="RequestController.requestMethod"></span>
                <input type="text" disabled="true" class="form-control" ng-model="RequestController.requestUrl"/>
              </div>
            </div>
          </div>

          <div class="form-group">
            <label class="col-sm-2 control-label"> Preview </label>
            <div class="col-sm-10">
              <div class="form-control" disabled="true">
                <em ng-repeat="urlParam in RequestController.urlParams  track by urlParam.value">/{{urlParam.value}}</em>
                <em ng-repeat="queryParam in RequestController.queryParams track by queryParam.value">
                    <span ng-show="$first">?{{queryParam.key}}={{queryParam.value}}</span>
                    <span ng-show="!$first">&amp;{{queryParam.key}}={{queryParam.value}}</span>
                </em>
              </div>
            </div>
          </div>

          <div class="form-group" ng-repeat="urlParam in RequestController.urlParams track by urlParam.value">
            <div ng-show="urlParam.key !== null">
              <label class="col-sm-2 control-label">
                Path Params
              </label>
              <div class="col-sm-10">
              <input type="text" class="form-control"
                     ng-model="urlParam.value"
                     placeholder="{{urlParam.key[0]}}"
                     />
              </div>
            </div>
          </div>

          <div class="form-group" ng-repeat="queryParam in RequestController.queryParams track by queryParam.value">
            <div ng-show="queryParam.key !== null">
              <label class="col-sm-2 control-label">
                Query Params
              </label>
              <div class="col-sm-10">
              <input type="text" class="form-control"
                     ng-model="queryParam.value" placeholder="{{queryParam.value}}"/>
              </div>
            </div>
          </div>

          <div class="form-group" ng-show="RequestController.requestMethod == 'POST' || RequestController.requestMethod == 'PUT'">
            <label class="col-sm-2 control-label"> POST or PUT Body </label>
            <div class="col-sm-10">
              <textarea class="form-control" cask-json-edit="RequestController.postBody" rows="5" cols="50"></textarea>
            </div>
          </div>
        </div>

        <div class="modal-footer">

          <div class="row">
            <div class="col-sm-4 col-sm-offset-2 text-left request-status">
              <span class="text-success" ng-if="RequestController.requestStatus === 'SUCCESS'">Request Successful</span>
              <span class="text-danger" ng-if="RequestController.requestStatus === 'ERROR'">Request Error</span>
            </div>
            <div class="col-sm-6">
              <button type="submit"
                      ng-disabled="makeRequestForm.$invalid || RequestController.loading"
                      class="btn"
                      ng-class="{'btn-success': !makeRequestForm.$invalid}">
                <span>Make Request</span>
                <span ng-if="RequestController.loading">
                  <i class="fa fa-refresh fa-spin"></i>
                </span>
              </button>
            </div>
          </div>

          <br/>

          <div class="form-group">
            <div class="col-sm-2 control-label"> Response </div>
            <div class="col-sm-10">
              <textarea class="form-control"
                        rows="10"
                        cols="50"
                        ng-bind="RequestController.response | json"
                        disabled="true">

              </textarea>
            </div>
          </div>
        </div>
      </form>

    </div>
  </div>
</div>
